package board.command;

import java.awt.*;
import java.util.*;

import sgf.SGFParseException;

import board.Board;

/**
 * Wraps a group of Commands so that they can be executed all at once.
 * @author TKington
 *
 */
public class CompositeCommand extends Command {
	/** The sub-commands. */
    private ArrayList<Command> cmds;
    
    /** Creates a new instance of CompositeCommand */
    public CompositeCommand() {
        cmds = new ArrayList<Command>();
    }
    
    /**
     * Creates a CompositeCommand from a list of Commands.
     * @param cmds the list of Commands
     */
    public CompositeCommand(ArrayList<Command> cmds) {
        this.cmds = cmds;
    }
    
    /**
     * Adds a Command to the CompositeCommand.
     * @param c the Command
     */
    public void add(Command c) { cmds.add(c); }
    
    /**
     * Gets a Command from the list contained in the CompositeCommand.
     * @param i the index into the list
     * @return the selected Command
     */
    public Command get(int i) { return cmds.get(i); }
    
    @Override
	public boolean doIt(Board b) {
        for(int i = 0; i < cmds.size(); i++) {
            Command c = cmds.get(i);
            c.doIt(b);
        }
        return true;
    }
    
    @Override
	public void undoIt(Board b) {
        for(int i = cmds.size() - 1; i >= 0; i--) {
            Command c = cmds.get(i);
            c.undoIt(b);
        }
    }
    
    @Override
	public Rectangle getBounds() {
        Rectangle ret = null;
        for(int i = 0; i < cmds.size(); i++) {
            Command c = cmds.get(i);
            if(ret == null)
                ret = c.getBounds();
            else ret = ret.union(c.getBounds());
        }
        return ret;
    }
    
    @Override
	public void validatePoints(int size) throws SGFParseException {
        for(int i = 0; i < cmds.size(); i++) {
            Command c = cmds.get(i);
            c.validatePoints(size);
        }
    }
}
